﻿package com.dhf.framework.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dhf.framework.base.mybatis.core.page.Pagination;
import com.dhf.framework.common.BaseController;
import com.dhf.framework.common.RequestParamToMap;
import com.dhf.framework.system.entity.SysTableColumn;
import com.dhf.framework.system.service.CommonService;
import com.dhf.framework.system.service.SysTableColumnService;

@Controller
@RequestMapping("/common")
public class CommonController extends BaseController {
	private static Logger LOGGER = LoggerFactory.getLogger(CommonController.class);

 	@Autowired
    private CommonService commonService;
 	
	@Autowired
	private SysTableColumnService sysTableColumnService;
 	
 	@Value("${tableSchema}")  
    private String tableSchema; 
 	
 	
 	/**
	  * 
	  * @Title: queryPageList
	  * @Description: 列表页面
	  * @param @param searchEntity
	  * @param @param pagination
	  * @return ModelAndView    返回类型
	  * @throws
	*/
	@RequestMapping(value = "/queryPageList/{tableName}")
	public ModelAndView queryPageList(@PathVariable String tableName,Pagination<Map<String,Object>> pagination,HttpServletRequest request) {
		LOGGER.debug(this.getClass().getName()+":开始查询分页数据！");
		ModelAndView mv = new ModelAndView( "system/common/commonList" );
		Map<String,Object> paraMap=RequestParamToMap.convert(request);
		paraMap.remove("pageNum");
		List<Map<String, Object>> tableFieldAttributes=this.commonService.queryTableFieldAttributes(tableSchema,tableName);
		pagination = this.commonService.queryPageList(paraMap, pagination,tableName);
		String primaryKey=this.commonService.queryTablePrimarykey(tableSchema, tableName);

		mv.addObject( "pagination", pagination );
		mv.addObject( "tableName", tableName );
		mv.addObject( "primaryKey", primaryKey );
		
		SysTableColumn searchEntity=new SysTableColumn();
		searchEntity.setTableName(tableName);
		searchEntity.setIsDisplay("1");
		List<SysTableColumn> displayList=this.sysTableColumnService.query(searchEntity);
		if(CollectionUtils.isNotEmpty(displayList)){
			List<Map<String, Object>> list=new ArrayList<Map<String, Object>>();
			for(SysTableColumn sysTableColumn:displayList){
				Map<String, Object> map=new HashMap<String, Object>();
				map.put("column_name", sysTableColumn.getColumnName());
				map.put("column_comment", sysTableColumn.getColumnComment());
				list.add(map);
			}
			mv.addObject( "dispalyList", list);
		}else{
			mv.addObject( "dispalyList", tableFieldAttributes);
		}
		
		
		
		SysTableColumn sysTableColumn=new SysTableColumn();
		sysTableColumn.setTableName(tableName);
		sysTableColumn.setIsQuery("1");
		List<SysTableColumn> queryList=this.sysTableColumnService.query(sysTableColumn);
		mv.addObject( "queryList", queryList );
		return mv;
	}
	
	
	/**
	 * 
	* @Title: toMerge
	* @Description: 新增或修改页面
	* @param tableName
	* @param entity
	* @return ModelAndView    返回类型
	* @throws
	 */
	@RequestMapping(value = "/toMerge/{tableName}")
	public ModelAndView toMerge(@PathVariable String tableName,HttpServletRequest request){
		ModelAndView mv = new ModelAndView("system/common/commonMerge");
		String primaryKey=this.commonService.queryTablePrimarykey(tableSchema, tableName);
		Map<String,Object> map=RequestParamToMap.convert(request);
		String keyValue=MapUtils.getString(map, primaryKey);
		List<Map<String, Object>> tableFieldAttributes=this.commonService.queryTableFieldAttributes(tableSchema,tableName);
				
		if(StringUtils.isNotEmpty(keyValue)){
			SysTableColumn sysTableColumn=new SysTableColumn();
			sysTableColumn.setTableName(tableName);
			sysTableColumn.setIsModify("1");
			List<SysTableColumn> addOrModifyList=this.sysTableColumnService.query(sysTableColumn);
			mv.addObject( "addOrModifyList", addOrModifyList );
			Map<String, Object> modelMap =  this.commonService.findOneData(tableName,RequestParamToMap.coventValues(map, tableFieldAttributes)) ;
			mv.addObject("tableFieldAttributes",tableFieldAttributes);
			mv.addObject("modelMap",modelMap);
		}else{
			for(Map<String, Object> attrMap:tableFieldAttributes){
				String key=MapUtils.getString(attrMap, "column_name");
				
				if(key.equals(primaryKey)){
					tableFieldAttributes.remove(attrMap);
					break;
				}
			}
			SysTableColumn sysTableColumn=new SysTableColumn();
			sysTableColumn.setTableName(tableName);
			sysTableColumn.setIsAdd("1");
			List<SysTableColumn> addOrModifyList=this.sysTableColumnService.query(sysTableColumn);
			mv.addObject("addOrModifyList", addOrModifyList );
			mv.addObject("tableFieldAttributes",tableFieldAttributes);
			mv.addObject("modelMap",null);
		}
		return mv;
	}
	
	/**
	 * 
	* @Title: doMerge
	* @Description: 新增或修改数据 
	* @param @param request
	* @param @param entity
	* @return ModelAndView    返回类型
	* @throws
	 */
	@RequestMapping(value = "/doMerge/{tableName}")
	public ModelAndView doMerge(@PathVariable String tableName,HttpServletRequest request,RedirectAttributes redirectAttributes) {
	    ModelAndView view=new ModelAndView( "redirect:/common/queryPageList/"+tableName+".do" );
		List<Map<String, Object>> tableFieldAttributes=this.commonService.queryTableFieldAttributes(tableSchema,tableName);
	    Map<String,Object> map1=RequestParamToMap.convert(request);
	    Map<String,Object> map=RequestParamToMap.coventValues(map1, tableFieldAttributes);
		String primaryKey=this.commonService.queryTablePrimarykey(tableSchema, tableName);
		String keyValue=MapUtils.getString(map, primaryKey);
		if (StringUtils.isNotEmpty(keyValue)) {
			LOGGER.debug(CommonController.class.getName()+":开始更新数据！");
			this.commonService.update(tableSchema,tableName, map);
			this.addMessage(redirectAttributes, "修改数据成功");
		} else {
			LOGGER.debug(CommonController.class.getName()+":开始新增数据！");
			this.commonService.add(tableSchema,tableName, map);
			this.addMessage(redirectAttributes, "新增据成功");
		}
		return view;
	}
	
	
	@RequestMapping(value = "/toDetail/{tableName}")
	public ModelAndView toDetail(@PathVariable String tableName,HttpServletRequest request){
		ModelAndView mv = new ModelAndView("system/common/commonDetail");
		Map<String,Object> map=RequestParamToMap.convert(request);
		List<Map<String, Object>> tableFieldAttributes=this.commonService.queryTableFieldAttributes(tableSchema,tableName);
		Map<String, Object> modelMap =  this.commonService.findOneData(tableName,RequestParamToMap.coventValues(map, tableFieldAttributes)) ;
		SysTableColumn sysTableColumn=new SysTableColumn();
		sysTableColumn.setTableName(tableName);
		sysTableColumn.setIsDetail("1");
		List<SysTableColumn> detailList=this.sysTableColumnService.query(sysTableColumn);
		mv.addObject("detailList",detailList);
		mv.addObject("tableFieldAttributes",tableFieldAttributes);
		mv.addObject("modelMap",modelMap);
		return mv;
	}
	
	@RequestMapping(value = "/delete/{tableName}")
	public ModelAndView delete(@PathVariable String tableName,HttpServletRequest request,RedirectAttributes redirectAttributes) {
		LOGGER.debug(CommonController.class.getName()+":开始删除数据！");
		Map<String,Object> map=RequestParamToMap.convert(request);
		this.commonService.delete(tableName, map);
        this.addMessage(redirectAttributes, "删除数据成功");
        return new ModelAndView( "redirect:/common/queryPageList/"+tableName+".do" );
	}
	
}
